//
//  BasePageViewController.m
//  BaseProject
//
//  Created by ctrl on 2019/8/8.
//  Copyright © 2019 qingzhou. All rights reserved.
//

#import "BasePageViewController.h"
#import "BaseListViewController.h"
@interface BasePageViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) UIView *pageView;
@property (nonatomic, strong) UIImageView *headerView;


@end

@implementation BasePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.pageScrollView];
    [self.pageScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self.pageScrollView reloadData];
}

#pragma mark - GKPageScrollViewDelegate
- (UIView *)headerViewInPageScrollView:(GKPageScrollView *)pageScrollView {
    return self.headerView;
}

- (UIView *)pageViewInPageScrollView:(GKPageScrollView *)pageScrollView {
    return self.pageView;
}

- (NSArray<id<GKPageListViewDelegate>> *)listViewsInPageScrollView:(GKPageScrollView *)pageScrollView {
    return self.childVCs;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.pageScrollView horizonScrollViewWillBeginScroll];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self.pageScrollView horizonScrollViewDidEndedScroll];
}

#pragma mark - 懒加载
- (UIView *)pageView {
    if (!_pageView) {
        _pageView = [UIView new];
        
        [_pageView addSubview:self.segmentView];
        [_pageView addSubview:self.contentScrollView];
    }
    return _pageView;
}

- (GKPageScrollView *)pageScrollView {
    if (!_pageScrollView) {
        _pageScrollView = [[GKPageScrollView alloc] initWithDelegate:self];
    }
    return _pageScrollView;
}

- (UIImageView *)headerView {
    if (!_headerView) {
        _headerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UNScreenWidth, UNBaseHeaderHeight)];
        _headerView.contentMode = UIViewContentModeScaleAspectFill;
        _headerView.clipsToBounds = YES;
        _headerView.image = [UIImage imageNamed:@"0"];
    }
    return _headerView;
}

- (JXCategoryTitleView *)segmentView {
    if (!_segmentView) {
                _segmentView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 0, UNScreenWidth, UNBaseSegmentHeight)];
                _segmentView.titles = @[@"动态", @"文章", @"更多"];
                _segmentView.titleFont = [UIFont systemFontOfSize:15.0f];
                _segmentView.titleSelectedFont = [UIFont systemFontOfSize:15.0f];
                _segmentView.titleColor = [UIColor grayColor];
                _segmentView.titleSelectedColor = [UIColor redColor];
                
                JXCategoryIndicatorLineView *lineView = [JXCategoryIndicatorLineView new];
                lineView.lineStyle = JXCategoryIndicatorLineStyle_Normal;
                lineView.indicatorLineViewHeight = ADAPTATIONRATIO * 4.0f;
                lineView.verticalMargin = ADAPTATIONRATIO * 2.0f;
                _segmentView.indicators = @[lineView];
                
                _segmentView.contentScrollView = self.contentScrollView;
                
                UIView  *btmLineView = [UIView new];
                btmLineView.backgroundColor = QZColorRGB(110, 110, 110);
                [_segmentView addSubview:btmLineView];
                [btmLineView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.right.bottom.equalTo(self.segmentView);
                    make.height.mas_equalTo(ADAPTATIONRATIO * 2.0f);
                }];
    }
            return _segmentView;
}

- (UIScrollView *)contentScrollView {
    if (!_contentScrollView) {
        CGFloat scrollW = UNScreenWidth;
        CGFloat scrollH = UNScreenHeight - UNNavBarHeight - UNBaseSegmentHeight;
        
        _contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, UNBaseSegmentHeight, scrollW, scrollH)];
        _contentScrollView.pagingEnabled = YES;
        _contentScrollView.bounces = NO;
        _contentScrollView.delegate = self;
        
        [self.childVCs enumerateObjectsUsingBlock:^(UIViewController *vc, NSUInteger idx, BOOL * _Nonnull stop) {
            [self addChildViewController:vc];
            [self->_contentScrollView addSubview:vc.view];
            
            vc.view.frame = CGRectMake(idx * scrollW, 0, scrollW, scrollH);
        }];
        _contentScrollView.contentSize = CGSizeMake(scrollW * self.childVCs.count, 0);
    }
    return _contentScrollView;
}

- (NSArray *)childVCs {
    if (!_childVCs) {
        BaseListViewController *dynamicVC = [BaseListViewController new];
        
        BaseListViewController *articleVC = [BaseListViewController new];
        
        BaseListViewController *moreVC = [BaseListViewController new];
        
        _childVCs = @[dynamicVC, articleVC, moreVC];
    }
    return _childVCs;
}

@end
